<?php
namespace app\wxwork\controller;

use app\wxwork\logic\WXWork;
use app\wxwork\logic\Contact;
use app\wxwork\logic\Message;
use cmf\controller\HomeBaseController;

class TestController extends HomeBaseController
{
    public function index()
    {
        return "测试";
    }
    public function getToken()
    {
        $wxwork=new WXWork();
        $token=$wxwork->getToken();
        if($token===false){
            $this->error($wxwork->getError());
        }
        return $token;
    }
    public function getDepartmentList()
    {
        $wxwork=new Contact();
        $data=$wxwork->getDepartmentList();
        if($data===false){
            $this->error($wxwork->getError());
        }
        var_dump($data);
    }
    public function getUserList()
    {
        $department_id=input("depa_id","1");
        $fetch_child=input("child",0);
        $wxwork=new Contact();
        $data=$wxwork->getUserList($department_id,$fetch_child);
        if($data===false){
            $this->error($wxwork->getError());
        }
        var_dump($data);
    }
    public function syncData()
    {
        $wxwork=new Contact();
        $data=$wxwork->syncData();
        if($data===false){
            $this->error($wxwork->getError());
        }
        var_dump($data);
    }
    public function sendMessage()
    {
        $content=input("message","hello world");
        $touesr=input("touser","GuoDong");
        $todepa=input("todepa");

        $agentid=config("wxwork_agentId");

        $wxwork=new Message();
        $target=$wxwork->makeSendTarget($touesr,$todepa);
        $message=$wxwork->makeTextMessage($content);
        $result=$wxwork->send($agentid,$target,$message);
        if($result===false){
            $this->error($wxwork->getError());
        }
        var_dump($result);
    }
    //发送文字卡片类型的消息
    public function sendMessage2()
    {
        $touesr=input("touser","GuoDong");
        $todepa=input("todepa");

        $agentid=config("wxwork_agentId");

        $wxwork=new Message();
        $target=$wxwork->makeSendTarget($touesr,$todepa);
        $message=$wxwork->makeTextCardMessage(
            "领奖通知",
            "<div class=\"gray\">2016年9月26日</div> <div class=\"normal\">恭喜你抽中iPhone 7一台，领奖码：xxxx</div><div class=\"highlight\">请于2016年10月10日前联系行政同事领取</div>",
            "http://www.airbaba.cn",
            "更多"
        );
        $result=$wxwork->send($agentid,$target,$message);
        if($result===false){
            $this->error($wxwork->getError());
        }
        var_dump($result);
    }
    public function receiveMessage()
    {
        $wxwork=new Message();
        $message=$wxwork->receive();
        $wxwork->reply("[自动回复]".$message);
    }
}
